package assignment2;
//Author Mark Mckillion, Date Completed 09/12/2011

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class QueueFrame extends JFrame
{   /*The purpose of this class is to open a frame which allows the user to
     *view how an implementation of a CircularArrayQueue would work.
     *The frame will have 5 buttons, a textual output area, a text field
     *that the user can input informtaion into and a panel which will show the
     *current objects in the queue including thier memory location.*/
    
    //Creating the GUI Components
    private JButton jButton1,jButton2,jButton3,jButton4,jButton5;
    private QueuePanel jPanel1; //Panel which I have created. See QueuePanel Class.
    private JTextField jTextField1;
    private TextArea textArea1;
    
    //Creating an instance of the CircularArrayQueue to record the user inputs and current status.
    private CircularArrayQueue <String> myQueue;
    
    {   //sets a title to the frame.
        this.setTitle("Circular Arry Queue Implementation");
        
        //Creating the GUI Components
        jButton1 = new JButton("Enqueue");
        jButton2 = new JButton("Dequeue");
        jButton3 = new JButton("First");
        jButton4 = new JButton("isEmpty");
        jButton5 = new JButton("Size");
        jPanel1 = new QueuePanel();
        jTextField1 = new JTextField();
        textArea1 = new TextArea("Use the buttons to control the queue program.");
        
        //Create an instance of CircularArrayQueue with the default number of String items.
        myQueue = new CircularArrayQueue <String> ();
        
        /*Set the queue in jPanel1 to equal the queue just created. 
         *This allows the queue to be equal and avoids public and static
         *objects, therefore minimizing data coupling.*/
        jPanel1.setQueue(myQueue);
        
        //Create action listeners for each of the buttons.
        class EnqueueListener implements ActionListener
        {   @Override
            public void actionPerformed (ActionEvent ae)
            {   /*This action listener will add an element onto the queue,
                 *replace the output in the TextArea and update jPanel1 to
                 *show the String added (using an integer, revalidate and 
                 *repaint to update the panel).*/
                if(myQueue.size() < 5 && !"".equals(jTextField1.getText()))
                {  /*This if statement controls the user input and only allows
                    *the user to input a String if the queue size is below 5.
                    *It also checks that the jTextField has a String in it.*/
                    myQueue.enqueue(jTextField1.getText());
                    textArea1.replaceRange(jTextField1.getText() + " has been put into the queue.", 0, 1000);
                    jPanel1.setCount(myQueue.size()); //sets the count value in QueuePanel.
                    jPanel1.setRear(myQueue.getRear()); //sets the rear value in QueuePanel.
                    jPanel1.revalidate();
                    jPanel1.repaint();
                }   else
                    {   /*If there is nothing in the jTextField this will 
                         *instruct the user to enter a String.*/
                        textArea1.replaceRange("Enter at least one character into the text box.",0,1000);
                    }
                if(myQueue.size() == 5)
                {   //If the queue is full (has 5 elements) this will inform the user.
                    textArea1.replaceRange("Queue collection is full.",0,1000);
                }
            }
        }
        
        class DequeueListener implements ActionListener
        {   @Override
            public void actionPerformed (ActionEvent ae)
            {   /*This action listener will remove an element from the queue,
                 *replace the output in the TextArea and update jPanel1 to
                 *show the String removed (using an integer, revalidate and 
                 *repaint to update the panel).*/
                if(!myQueue.isEmpty())
                {   /*This if statement controls the user input and only allows
                     *the user to input a String if the queue is not empty.*/
                    //Prints the first item before dequeuing it.
                    textArea1.replaceRange(myQueue.first() + " has been removed from the queue.", 0, 1000);
                    myQueue.dequeue();
                    jPanel1.setCount(myQueue.size()); //sets the count value in QueuePanel.
                    jPanel1.revalidate();
                    jPanel1.repaint();
                }   else
                    {   //If the queue is empty this will instruct the user to add a String using enqueue.
                        textArea1.replaceRange("Add an element using the Enqueue button.", 0, 1000); 
                    }
            }
        }
        
        class FirstListener implements ActionListener
        {   @Override
            public void actionPerformed (ActionEvent ae)
            {   /*This action listener will return the first element in the
                 *queue and replace the output in TextArea to show the user.*/
                if(!myQueue.isEmpty())
                {   /*This if statment says if the queue is not empty, create a
                     *String, make that String equal the first element of the
                     *queue then replace the text in Text Area to show the
                     *string.*/
                    String text = (String)myQueue.first();
                    textArea1.replaceRange("The first element of the queue is " + "'" + text + "'.", 0, 1000);
                }   else
                    {   //If the stack is empty this will instruct the user to add a String using push.
                        textArea1.replaceRange("There are no elements, add elements using the enqueue button.", 0, 1000);                         
                    }
            }
        }
        
        class IsEmptyListener implements ActionListener
        {   @Override
            public void actionPerformed (ActionEvent ae)
            {   //This action listener will tell the user if the stack is empty.
                if(myQueue.isEmpty())
                {   /*This if statement determains whether the queue is empty.
                     *It then informs the user of this in the Text Area*/
                    textArea1.replaceRange("The current queue is empty.", 0, 1000);                    
                }   else
                    {   textArea1.replaceRange("The current queue is not empty.", 0, 1000);
                    }
            }
        }
        
        class SizeListener implements ActionListener
        {   @Override
            public void actionPerformed (ActionEvent ae)
            {   //This action listener will output the size of the queue.
                if(myQueue.isEmpty())
                {   /*This if statement determains the size of the queue and outputs
                     *it into the text area*/
                    textArea1.replaceRange("The current queue has 0 elements.", 0, 1000);                    
                }   else
                    {   textArea1.replaceRange("The current queue has " + myQueue.size() + " elements.", 0, 1000);
                    }
            }
        }
        
        //add the action listeners to the buttons.
        jButton1.addActionListener(new EnqueueListener());
        jButton2.addActionListener(new DequeueListener());
        jButton3.addActionListener(new FirstListener());
        jButton4.addActionListener(new IsEmptyListener());
        jButton5.addActionListener(new SizeListener());   
        
        //used a swing GUI form to create the GroupLayout, and copied the source code.
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(242, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(textArea1, javax.swing.GroupLayout.DEFAULT_SIZE, 650, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(5, 5, 5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textArea1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
        ); pack();  
    }
}
